import * as THREE from 'three'
import { mainStore } from '@store'


const selectionBox = new THREE.Box3();

let startX, startY, endX, endY, store
/**
 * 禁止场景旋转
 * @param 无传参
 * @returns 无返回值
 */

export const frameSelectFun = () => {
  store = mainStore()

  // 开始框选 由于触发机制 需要线判断为false的情况
  if (!store.captureData.show) {
    window.ThreePerceivedEffect.controls.enableRotate = false
    window.ThreePerceivedEffect.renderer.domElement.addEventListener('mousedown', handleDown)
  }
  if (store.captureData.show) handleUm()
}

const handleDown = (e) => {
  startX = e.offsetX
  startY = e.offsetY
  // startPoint = getMousePosition(e.offsetX, e.offsetY);
  store.captureData = {
    show: true,
    top: e.offsetY,
    left: e.offsetX,
    width: 2,
    height: 2,
  }
  document.addEventListener('mousemove', handleMove)
  document.addEventListener('mouseup', handleUp)
}

const handleMove = (e) => {
  endX = e.offsetX
  endY = e.offsetY
  store.captureData.width = endX - startX
  store.captureData.height = endY - startY
  // endPoint = getMousePosition(e.offsetX, e.offsetY);
  updateSelectionBox();
}

const handleUp = (e) => {
  // endPoint = getMousePosition(e.offsetX, e.offsetY);
  updateSelectionBox();
  document.removeEventListener('mousemove', handleMove)
  document.removeEventListener('mouseup', handleUp)
  getPointsInBox();
}

// 将屏幕坐标转换为三维世界坐标
function screenToWorld(x, y) {
  const mouse = new THREE.Vector2();
  mouse.x = (x / window.ThreePerceivedEffect.container.offsetWidth) * 2 - 1;
  mouse.y = -(y / window.ThreePerceivedEffect.container.offsetHeight) * 2 + 1;

  const raycaster = new THREE.Raycaster();
  raycaster.setFromCamera(mouse, window.ThreePerceivedEffect.camera);

  // 假设我们有一个平面来检测交点，这里使用一个无限大的平面
  const plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
  const ray = raycaster.ray;
  const intersectionPoint = new THREE.Vector3();
  if (ray.intersectPlane(plane, intersectionPoint)) {
    // console.log(intersectionPoint);
    return intersectionPoint;
  }
}

function updateSelectionBox() {
    const start = screenToWorld(startX, startY)
    const end = screenToWorld(endX, endY)
    // const axesHelper = new THREE.AxesHelper(500);
    // axesHelper.position.set(start.x, start.y, 0);
    // window.ThreePerceivedEffect.scene.add(axesHelper);
    const minX = Math.min(start.x, end.x);
    const maxX = Math.max(start.x, end.x);
    const minY = Math.min(start.y, end.y);
    const maxY = Math.max(start.y, end.y);

    selectionBox.min.set(minX, minY, -1000);
    selectionBox.max.set(maxX, maxY, 1000);
}

function getPointsInBox() {
  // window.ThreePerceivedEffect.scene.getObjectByName('pcd')
  let positions = window.ThreePerceivedEffect.points.geometry.attributes.position.array;
    const pointsInBox = [];
    // const positions = .geometry.attributes.position.array;
    for (let i = 0; i < positions.length; i += 3) {
        const point = new THREE.Vector3(positions[i], positions[i + 1], positions[i + 2]);
        if (selectionBox.containsPoint(point)) {
            pointsInBox.push(point);
        }
    }
    store.update_pointsInData(pointsInBox)
}

export const handleUm = () => {
  if (!store) return // 多种调用情况
  window.ThreePerceivedEffect.controls.enableRotate = true
  window.ThreePerceivedEffect.renderer.domElement.removeEventListener('mousedown', handleDown)
  store.captureData.show = false
  store.captureData.showBtn = false
}